1 //+------------------------------------------------------------------+
\r
2 //| SpeedTrain Zarko.mq4 |
\r
3 //| Copyright © 2011, MetaQuotes Software Corp. |
\r
4 //| http://www.metaquotes.net |
\r
5 //+------------------------------------------------------------------+
\r
6 #property copyright "Copyright © 2011, MetaQuotes Software Corp."
\r
7 #property link "http://www.metaquotes.net"
\r
10 #include <stderror.mqh>
\r
11 #include <stdlib.mqh>
\r
15 extern int Slippage = 3;
\r
20 extern double LotSize= 0.1;
\r
22 extern string PipStep_Remarks="------ Pip Distance. 30 pips is 30 pips for all brokers -------";
\r
23 extern double PipStep= 50;
\r
25 extern string MoveSL2BE_Remarks="------ Move Prev order SL to BE ------";
\r
26 extern bool MoveSL2BE = true;
\r
28 extern string InitialBuyOrder_Remarks="------ First Order on start, if true its LONG ------";
\r
29 extern bool InitialBuyOrder = true;
\r
31 extern string UseTradingHours_Remarks="------ Place orders only during these hours. however closing can happen any time ------";
\r
32 extern bool UseTradingHours = false;
\r
33 extern int StartHour = 7;
\r
34 extern int FinishHour = 19;
\r
36 extern string ProfitPct_Remarks="------ Profit Percentage ------";
\r
37 extern double Profit_Fixed= 100.0;
\r
38 extern double Panic_At = 80.0;
\r
39 extern bool Paranoid_Stop_Loss = true;
\r
41 extern string Bollinger_Bands_Remarks = "Difference between upper and lower Bollinger bounds must be above the value in order to place trades.";
\r
42 extern bool Use_Bollinger_Bands = true;
\r
43 extern double Min_Bollinger_Bands_Width = 0.02;
\r
44 extern int Bollinger_Band_Days = 30;
\r
45 extern int Bollinger_Band_Deviation = 3;
\r
47 extern int MagicNumber = 1771611;
\r
49 int longOrderCount=0;
\r
50 int shortOrderCount=0;
\r
51 double DollarProfitTarget;
\r
52 double valLastFlatEq;
\r
53 double valTargetFlatEq;
\r
55 bool longBias=false;
\r
57 //double currentPL=0;
\r
59 double startPrice=0.0;
\r
61 double avgEntryPrice=0.0;
\r
66 bool initOrdersDone=false;
\r
70 digits=MarketInfo(Symbol(),MODE_DIGITS);
\r
84 //---- TODO: add your code here
\r
93 for(int j = 0; j <= 10000; j++){
\r
94 ObjectDelete("Comment" + j);
\r
103 //DollarProfitTarget = StrToDouble(DoubleToStr(AccountEquity()*ProfitPct/100.0,2));
\r
104 if(startPrice ==0.0) {
\r
105 startPrice=iOpen( NULL, PERIOD_M15, 0);
\r
106 Print("----- startPrice ...... "+startPrice);
\r
109 if(!initOrdersDone && inTradingHours()){
\r
112 placeBuyStopOrder();
\r
113 placeSellStopOrder();
\r
114 if(InitialBuyOrder) {
\r
116 placeInitBuyOrder();
\r
118 placeInitSellOrder();
\r
122 initOrdersDone=true;
\r
125 double currentPL = getEAPL();
\r
126 bool panic_mode = false;
\r
128 // if (!CloseAll && currentPL > DollarProfitTarget)
\r
130 if (currentPL+AccountBalance() < Panic_At) panic_mode = true;
\r
133 (currentPL+AccountBalance() > valTargetFlatEq ||
\r
134 (panic_mode == true && currentPL > DollarProfitTarget)
\r
137 Print("================ closing ================");
\r
138 Print("currentPL "+currentPL+" AccountBalance "+AccountBalance()+" valTargetFlatEq "+valTargetFlatEq);
\r
144 if(inTradingHours()) {
\r
146 if (Paranoid_Stop_Loss) {
\r
147 double servers_min_stop = (Slippage + MarketInfo(Symbol(), MODE_STOPLEVEL)) * Point;
\r
148 if(iClose( NULL, PERIOD_M15, 0) > startPrice + servers_min_stop) {
\r
149 if(MoveSL2BE) moveSellOrderToBE();
\r
150 } else if(iClose( NULL, PERIOD_M15, 0) < startPrice - servers_min_stop) {
\r
151 if(MoveSL2BE) moveBuyOrderToBE();
\r
155 if(iClose( NULL, PERIOD_M15, 0) > (startPrice + PipStep*pip)){
\r
157 startPrice=iClose( NULL, PERIOD_M15, 0);
\r
158 Print("----- moved to startPrice ...... "+startPrice);
\r
159 // need to set the sl to BE of last order
\r
160 if(MoveSL2BE) moveSellOrderToBE();
\r
161 CloseAllPendingOrders();
\r
162 // place buy limit order
\r
164 placeBuyStopOrder();
\r
165 placeSellStopOrder();
\r
167 } else if(iClose( NULL, PERIOD_M15, 0) < (startPrice - PipStep*pip)){
\r
169 startPrice=iClose( NULL, PERIOD_M15, 0);
\r
170 Print("------ moved to startPrice ...... "+startPrice);
\r
171 // need to set the sl to BE of last order
\r
172 if(MoveSL2BE) moveBuyOrderToBE();
\r
173 CloseAllPendingOrders();
\r
174 // place buy limit order
\r
176 placeSellStopOrder();
\r
177 placeBuyStopOrder();
\r
183 printComment(currentPL);
\r
186 bool inTradingHours(){
\r
188 bool returnVal = true;
\r
190 if (UseTradingHours)
\r
192 if (TimeHour(iTime( NULL, 0, 0) ) < StartHour &&
\r
193 ((TimeHour(iTime( NULL, 0, 0)) > FinishHour && FinishHour != 0) ||
\r
194 (TimeHour(iTime( NULL, 0, 0)) < 24 && FinishHour == 0)))
\r
206 bool returnVal = true;
\r
208 if (Use_Bollinger_Bands)
\r
210 double upper_band = iBands(NULL, PERIOD_M15, Bollinger_Band_Days, Bollinger_Band_Deviation, 0, PRICE_CLOSE, MODE_UPPER, 0);
\r
211 double lower_band = iBands(NULL, PERIOD_M15, Bollinger_Band_Days, Bollinger_Band_Deviation, 0, PRICE_CLOSE, MODE_LOWER, 0);
\r
213 Print("upper_band: " + upper_band + " lower_band: " + lower_band);
\r
214 if (upper_band - lower_band < Min_Bollinger_Bands_Width) returnVal = false;
\r
220 void printComment(double currentPL){
\r
223 double curPL = -valLastFlatEq + AccountBalance()+currentPL;
\r
224 double drawdown = (curPL/valLastFlatEq)*100;
\r
226 string commentSt = "\nEquity "+DoubleToStr(valLastFlatEq,0) + " Dollar Profit "+DoubleToStr(DollarProfitTarget,2)+" PL " +DoubleToStr(curPL,2)
\r
227 +" DD "+DoubleToStr(drawdown,2);
\r
229 ObjectCreate("CommentStr",OBJ_LABEL,0,0,0);
\r
230 ObjectSet("CommentStr",OBJPROP_XDISTANCE,5);
\r
231 ObjectSet("CommentStr",OBJPROP_YDISTANCE,40);
\r
232 ObjectSet("CommentStr",OBJPROP_WIDTH,5);
\r
233 ObjectSetText("CommentStr",commentSt,12,"Arial",DodgerBlue);
\r
235 string bias="Short";
\r
236 double targetPrice=0;
\r
239 if(totalOrders>0) {
\r
241 // Print("profit "+DollarProfitTarget+" totalOrders "+totalOrders+" LotSize "+LotSize+" qty "+MarketInfo(Symbol(), MODE_LOTSIZE) +" avgEntryPrice "+avgEntryPrice+
\r
242 // " bottom "+totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE) +" top "+(DollarProfitTarget*pip));
\r
244 targetPrice = (DollarProfitTarget/(totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE)))+avgEntryPrice;
\r
247 if(totalOrders>0) {
\r
248 // Print("totalOrders "+totalOrders+" LotSize "+LotSize+" qty "+MarketInfo(Symbol(), MODE_LOTSIZE) +" avgEntryPrice "+avgEntryPrice);
\r
250 targetPrice = -(DollarProfitTarget/(totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE)))+avgEntryPrice;
\r
254 string commentSt1 = "\nBias "+bias + ", Orders "+totalOrders+", Target " +DoubleToStr(targetPrice,4) ;
\r
256 ObjectCreate("CommentStr1",OBJ_LABEL,0,0,0);
\r
257 ObjectSet("CommentStr1",OBJPROP_XDISTANCE,5);
\r
258 ObjectSet("CommentStr1",OBJPROP_YDISTANCE,60);
\r
259 ObjectSet("CommentStr1",OBJPROP_WIDTH,5);
\r
260 ObjectSetText("CommentStr1",commentSt1,12,"Arial",MediumSpringGreen);
\r
265 void resetAccountCalc(){
\r
266 DollarProfitTarget = Profit_Fixed;
\r
267 valLastFlatEq = AccountBalance();
\r
268 valTargetFlatEq = valLastFlatEq + DollarProfitTarget;
\r
269 Print("\nDollarProfitTarget ...... "+DollarProfitTarget+" valLastFlatEq "+valLastFlatEq);
\r
273 //+------------------------------------------------------------------+
\r
274 //| Close all of the open trades. |
\r
275 //+------------------------------------------------------------------+
\r
277 void CloseAllTrades()
\r
280 for(int i = 0; i < OrdersTotal(); i++)
\r
282 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
284 if (OrderMagicNumber() == MagicNumber)
\r
286 if (OrderType() == OP_BUY)
\r
288 Comment("In full closure mode. Closing a ", OrderSymbol(), " buy trade...");
\r
289 Print("======================================================== In full closure mode. Closing a ", OrderSymbol(), " buy trade...");
\r
290 bool closeStatus = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),Slippage,Blue);
\r
291 if (!closeStatus) {
\r
292 Print("Close Buy failed with error #",GetLastError());
\r
293 int check=GetLastError();
\r
294 if(check!=0) Print("Close Buy failed with error: ",ErrorDescription(check));
\r
299 if (OrderType() == OP_SELL)
\r
301 Comment("In full closure mode. Closing a ", OrderSymbol(), " sell trade...");
\r
302 Print("============================================================In full closure mode. Closing a ", OrderSymbol(), " sell trade...");
\r
303 closeStatus = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),Slippage,Red);
\r
304 if (!closeStatus) {
\r
305 Print("Close Sell failed with error #",GetLastError());
\r
306 check=GetLastError();
\r
307 if(check!=0) Print("Close Sell failed with error: ",ErrorDescription(check));
\r
310 }else if (OrderType() == OP_BUYSTOP)
\r
312 Comment("In full closure mode. Closing pending order ", OrderSymbol(), " buy trade...");
\r
313 Print("======================================================== In full closure mode. Closing a ", OrderSymbol(), " buy trade...");
\r
314 closeStatus = OrderDelete(OrderTicket(),Blue);
\r
315 if (!closeStatus) {
\r
316 Print("Close Buy Stop failed with error #",GetLastError());
\r
317 check=GetLastError();
\r
318 if(check!=0) Print("Close Buy Stop failed with error: ",ErrorDescription(check));
\r
323 if (OrderType() == OP_SELLSTOP)
\r
325 Comment("In full closure mode. Closing pending order ", OrderSymbol(), " sell trade...");
\r
326 Print("============================================================In full closure mode. Closing a ", OrderSymbol(), " sell trade...");
\r
327 closeStatus=OrderDelete(OrderTicket(),Red);
\r
328 if (!closeStatus) {
\r
329 Print("Close Sell Stop with error #",GetLastError());
\r
330 check=GetLastError();
\r
331 if(check!=0) Print("Close Sell Stop with error: ",ErrorDescription(check));
\r
338 resetAccountCalc();
\r
339 startPrice=iOpen( NULL, PERIOD_M15, 0);
\r
340 Print("resetting startPrice ...... "+startPrice);
\r
341 initOrdersDone=false;
\r
346 void CloseAllPendingOrders()
\r
349 for(int i = 0; i < OrdersTotal(); i++)
\r
351 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
353 if (OrderMagicNumber() == MagicNumber)
\r
355 // Print(" order type "+OrderType());
\r
356 if (OrderType() == OP_BUYSTOP)
\r
358 Comment("..... Closing pending order ", OrderSymbol(), " buy trade...");
\r
359 Print("======================================================== Closing a ", OrderSymbol(), " buy stop order...");
\r
360 bool closeStatus = OrderDelete(OrderTicket(),Blue);
\r
361 if (!closeStatus) {
\r
362 Print("Close Buy Stop failed with error #",GetLastError());
\r
363 int check=GetLastError();
\r
364 if(check!=0) Print("Close Buy Stop failed with error: ",ErrorDescription(check));
\r
369 if (OrderType() == OP_SELLSTOP)
\r
371 Comment("..... Closing pending order ", OrderSymbol(), " sell trade...");
\r
372 Print("=========================================================== Closing a ", OrderSymbol(), " sell stop order...");
\r
373 closeStatus=OrderDelete(OrderTicket(),Red);
\r
374 if (!closeStatus) {
\r
375 Print("Close Sell Stop with error #",GetLastError());
\r
376 check=GetLastError();
\r
377 if(check!=0) Print("Close Sell Stop with error: ",ErrorDescription(check));
\r
392 double totalEntryPrice=0.0;
\r
395 for(int i = 0; i < OrdersTotal(); i++)
\r
397 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
399 if (OrderMagicNumber() == MagicNumber && ( (OrderType() == OP_BUY)||(OrderType() == OP_SELL)))
\r
402 double curPL = OrderProfit() + OrderSwap() + OrderCommission();
\r
403 double openPrice = OrderOpenPrice();
\r
404 // Print(" openPrice "+openPrice);
\r
405 totalEntryPrice = totalEntryPrice + openPrice;
\r
406 // Print(" totalEntryPrice "+totalEntryPrice);
\r
407 // Print("curPL "+curPL);
\r
408 overallPL = overallPL + curPL;
\r
414 if(totalOrders>0) {
\r
415 avgEntryPrice = totalEntryPrice/totalOrders;
\r
416 //Print(" avgEntryPrice "+avgEntryPrice+" totalOrders "+totalOrders+" totalEntryPrice "+totalEntryPrice);
\r
424 void placeSellStopOrder(){
\r
426 double bid = MarketInfo(Symbol(), MODE_BID) ;
\r
428 double sellPrice = bid - (PipStep*pip);
\r
429 double sellSL = bid;
\r
432 Print("Placing sell stop order at "+sellPrice+" with SL at "+sellSL);
\r
434 int ticketshort = OrderSend(Symbol(),OP_SELLSTOP,LotSize,NormalizeDouble(sellPrice, digits),
\r
435 Slippage,NormalizeDouble(sellSL, digits),0,"Short Speed Train",MagicNumber,0,Red);
\r
437 if (ticketshort < 0) {
\r
438 // Print("Short OrderSend OP_SELLSTOP failed with error #",GetLastError());
\r
439 int check=GetLastError();
\r
440 if(check!=0) Print("Short OrderSend OP_SELLSTOP failed with error: ",ErrorDescription(check));
\r
444 Print("Sell stop order placed with ticket "+ticketshort+" at "+sellPrice);
\r
453 void placeBuyStopOrder(){
\r
455 double ask = MarketInfo(Symbol(), MODE_ASK) ;
\r
457 double buyPrice = ask + (PipStep*pip);
\r
458 double buySL = ask;
\r
461 Print("Placing buy stop order at "+buyPrice+" with SL at "+buySL);
\r
463 int ticketlong = OrderSend(Symbol(),OP_BUYSTOP,LotSize,NormalizeDouble(buyPrice, digits),
\r
464 Slippage,NormalizeDouble(buySL, digits),0,"Long Speed Train",MagicNumber,0,Blue);
\r
466 if (ticketlong < 0) {
\r
467 //Print("Long OrderSend OP_BUYSTOP failed with error #",GetLastError());
\r
469 int check=GetLastError();
\r
470 if(check!=0) Print("Long OrderSend OP_BUYSTOP failed with error: ",ErrorDescription(check));
\r
476 Print("Buy stop order placed with ticket "+ticketlong+" at "+buyPrice);
\r
486 void placeInitBuyOrder(){
\r
488 double ask = MarketInfo(Symbol(), MODE_ASK) ;
\r
490 double buySL = ask - (PipStep*pip);
\r
494 Print("Placing buy order at "+ask+" with SL at "+buySL);
\r
496 int ticketlong = OrderSend(Symbol(),OP_BUY,LotSize,Ask,
\r
497 Slippage,0,0,"Long Speed Train",MagicNumber,0,Blue);
\r
499 if (ticketlong < 0) {
\r
500 // Print("Long OrderSend OP_BUY failed with error #",GetLastError());
\r
501 int check=GetLastError();
\r
502 if(check!=0) Print("Long OrderSend OP_BUY failed with error: ",ErrorDescription(check));
\r
506 Print("Buy stop order placed with ticket "+ticketlong+" at "+ask);
\r
508 int ticketlong1 = OrderModify(ticketlong,OrderOpenPrice(),NormalizeDouble(buySL, digits),0,0,Blue);
\r
511 // Print("ticketlong1 "+ticketlong1);
\r
513 if (ticketlong1 < 1) {
\r
514 // Print("Long OrderModify failed with error #",GetLastError());
\r
515 check=GetLastError();
\r
516 // Print("check "+check);
\r
517 if(check!=0) Print("Long OrderModify failed with error: ",ErrorDescription(check));
\r
526 void placeInitSellOrder(){
\r
528 double bid = MarketInfo(Symbol(), MODE_BID) ;
\r
530 double sellSL = bid + (PipStep*pip);
\r
532 Print("Placing sell order at "+bid+" with SL at "+sellSL);
\r
534 int ticketshort = OrderSend(Symbol(),OP_SELL,LotSize,NormalizeDouble(bid, digits),
\r
535 Slippage,0,0,"Short Speed Train",MagicNumber,0,Red);
\r
537 if (ticketshort < 0) {
\r
538 //Print("Short OrderSend OP_SELL failed with error #",GetLastError());
\r
539 int check=GetLastError();
\r
540 if(check!=0) Print("Short OrderSend OP_SELL failed with error: ",ErrorDescription(check));
\r
544 Print("Sell stop order placed with ticket "+ticketshort+" at "+bid);
\r
546 int ticketshort1 = OrderModify(ticketshort,OrderOpenPrice(),NormalizeDouble(sellSL, digits),0,0,Red);
\r
549 if (ticketshort1 < 1) {
\r
550 // Print("Short OrderModify failed with error #",GetLastError());
\r
551 check=GetLastError();
\r
552 if(check!=0) Print("Short OrderModify failed with error: ",ErrorDescription(check));
\r
555 shortOrderCount++;
\r
560 void moveBuyOrderToBE(){
\r
563 for(int j = 0; j < OrdersTotal(); j++)
\r
565 OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
\r
566 // Print("order type "+OrderType());
\r
568 if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUYSTOP)
\r
570 int ticketId = OrderTicket() ;
\r
571 //Print("ticketId "+ticketId);
\r
572 if( ticketId> lastTicket) lastTicket=ticketId;
\r
574 //Print("lastTicket "+lastTicket);
\r
578 if(lastTicket >0) {
\r
579 Print(" ----------------------- Moving long order number "+lastTicket+ " to BE at "+OrderOpenPrice());
\r
581 int ticketlong = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Blue);
\r
584 if (ticketlong < 0) {
\r
585 // Print("Long SL2BE order failed with error #",GetLastError());
\r
586 int check=GetLastError();
\r
587 if(check!=0) Print("Long SL2BE order failed with error: ",ErrorDescription(check));
\r
603 void moveSellOrderToBE(){
\r
606 for(int j = 0; j < OrdersTotal(); j++)
\r
608 OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
\r
610 // Print("order type "+OrderType());
\r
611 if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELLSTOP)
\r
613 int ticketId = OrderTicket() ;
\r
614 // Print(" ticketId "+ticketId);
\r
615 if( ticketId> lastTicket) lastTicket=ticketId;
\r
616 // Print(" lastTicket "+lastTicket);
\r
626 Print("------------ Moving short order number "+lastTicket+ " to BE at "+OrderOpenPrice());
\r
627 int ticketshort = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Red);
\r
629 if (ticketshort < 0) {
\r
630 //Print("Short SL2BE order failed with error #",GetLastError());
\r
631 int check=GetLastError();
\r
632 if(check!=0) Print("Short SL2BE order failed with error: ",ErrorDescription(check));
\r